package com.yqb.study.common.from.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.yqb.study.util.ValidatorUtil;

/**
 * 校验手机号的过程
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

	private boolean required = false;

	public void initialize(IsMobile constraintAnnotation) {
		required = constraintAnnotation.required();
	}

	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (required) {
			// 参数必定存在不为null直接校验
			return ValidatorUtil.isMobile(value);
		} else {
			if (StringUtils.isEmpty(value)) {
				// 参数允许为空,确实是空返回true
				return true;
			} else {
				return ValidatorUtil.isMobile(value);
			}
		}
	}
}
